package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb04Kszl;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb04KszlExc;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb04KszlService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿石种类
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "矿石种类")
@RestController
@RequestMapping("/dictionary/cb04Kszl")
@Validated
public class Cb04KszlController {

    @Resource
    private ICb04KszlService iCb04KszlService;

    /**
     * 添加矿石种类
     */
    @ApiOperation("添加矿石种类")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb04Kszl cb04Kszl) {
        iCb04KszlService.save(cb04Kszl);
        return CommonResult.ok();
    }

    /**
     * 删除矿石种类
     */
    @ApiOperation("删除矿石种类")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb04KszlService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑矿石种类
     */
    @ApiOperation("编辑矿石种类")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb04Kszl cb04Kszl) {
        iCb04KszlService.updateById(cb04Kszl);
        return CommonResult.ok();
    }

    /**
     * 矿石种类分页查询
     */
    @ApiOperation("矿石种类分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb04Kszl>> page() {
        return CommonResult.data(iCb04KszlService.page(new Cb04Kszl()));
    }

    /**
     * 矿石质量筛选下拉框
     */
    @ApiOperation("矿石质量筛选下拉框")
    @GetMapping("/kszlList")
    public CommonResult<List<Cb04Kszl>> kszlList(
            @ApiParam(name = "kcdm", value = "矿区代码",  required = true) @RequestParam Integer kcdm
    ) {
        return CommonResult.data(iCb04KszlService.getKszlList(kcdm));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb04KszlExc> list = ExcelImportUtil.readMultipartFile(file, Cb04KszlExc.class);
            List<Cb04Kszl> cb01Zhs = GsonUtil.ObjectToList(list, Cb04Kszl.class);
            iCb04KszlService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
